package com.hp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.Good;
import com.hp.entity.Item;
import com.hp.entity.Order;
import com.hp.entity.User;
import com.hp.service.IGoodService;
import com.hp.service.IItemService;
import com.hp.service.IOrderService;
import com.hp.service.IUserService;
import com.hp.vo.ItemVo;
import com.hp.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Admin
 * @Date: 2021/10/26 10:37
 */
@Controller
@RequestMapping("/admin")
public class OrderController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IItemService itemService;
    @Autowired
    private IGoodService goodService;

    /**
     * 跳转订单页面并查询订单信息
     * @param model
     * @return
     */
    @GetMapping("/orderList")
    public String orderList(@RequestParam("page") Integer page,Integer status, Model model){
        PageHelper.startPage(page,4);
        //查询全部orders表信息
        List<Order> orderList = orderService.findOrderAll(status);
        List<OrderVo> orderVoList = copyBean(orderList);
        PageInfo pageInfo = new PageInfo(orderList);
        pageInfo.setList(orderVoList);
        model.addAttribute("pageInfo",pageInfo);
        return "admins/order_list";
    }

    /**
     * 实体类复制vo
     * @param orderList
     * @return
     */
    private List<OrderVo> copyBean(List<Order> orderList){
        //定义一个OrderVo集合
        List<OrderVo> orderVoList = new ArrayList<>();
        for (Order order : orderList){
            //每循环一次创建新的orderVo
            OrderVo orderVo = new OrderVo();
            //将orderList的子实体类信息赋值到orderVo
            BeanUtils.copyProperties(order,orderVo);
            //根据orders的id查询到items信息
            List<Item> itemList = itemService.findItemById(order.getId());
            List<ItemVo> itemVoList = new ArrayList<>();
            for (Item item : itemList){
                ItemVo itemVo = new ItemVo();
                //根据items表的good_id查询到goods信息name
                Good good = goodService.findGoodById(item.getGoodId());
                BeanUtils.copyProperties(item,itemVo);
                //单独设置goodName信息
                itemVo.setGoodName(good.getName());
                itemVoList.add(itemVo);
            }
            orderVo.setItemVo(itemVoList);
            //得到用户名信息
            User user = userService.findUserById(order.getUserId());
            orderVo.setUser(user);
            orderVoList.add(orderVo);
        }
        return orderVoList;
    }

    /**
     * 修改订单状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/orderUpdate")
    public String orderUpdate(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        //根据id修改状态码
        orderService.orderUpdate(id,status);
        return "redirect:/admin/orderList?page=1&status="+status;
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @GetMapping("/orderDelete")
    public String orderDelete(@RequestParam("id") Integer id){
        //根据id删除orders信息
        orderService.orderDelete(id);
        //根据order_id删除items信息
        itemService.itemDeleteById(id);
        return "redirect:/admin/orderList?page=1";
    }
}
